﻿using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Windows.Media;
using OpenLS.Drawing;

namespace OpenLS.Spreadsheet.UI
{
    class BackgroundFormatter : FormatterBase
    {
        private readonly DifferentialStyleRecord _record;

        public BackgroundFormatter(FormattingRecord record)
        {
            this._background = record.Background;
            Contract.Assert(
    !(_background is GradientFill &&
      ((GradientFill)_background).Stops.Count == 0));

        }
        public BackgroundFormatter(DifferentialStyleRecord record)
        {
            _record = record;
            this._background = record.Background;
            Contract.Assert(
                !(_background is GradientFill &&
                  ((GradientFill)_background).Stops.Count == 0));

        }

        private Fill _background;
        public Fill Background
        {
            get
            {
                Contract.Ensures(
                    !(Contract.Result<Fill>() is GradientFill &&
                      ((GradientFill) Contract.Result<Fill>()).Stops.Count == 0));
                return _background;
            }
            set
            {
                Contract.Assert(
    !(value is GradientFill &&
      ((GradientFill)value).Stops.Count == 0));

                _background = value;
                if (_record != null)
                    _record.Background = value;
                notifyPropertyChanged("Background"); SetDirty();
            }
        }
        protected override void ApplyOverride(Range selection)
        {
            selection.Background = Background;
        }
        public IEnumerable<SpreadsheetColor> AllColors
        {
            get
            {
                {
                    foreach (ColorSchemeIndex i in Indexes)
                        yield return (new ThemeColor(i, null));
                    yield return (new ThemeColor(ColorSchemeIndex.Dark1, 0.5));
                    yield return (new ThemeColor(ColorSchemeIndex.Light1, -0.05));
                    foreach (ColorSchemeIndex i in Indexes2)
                        yield return (new ThemeColor(i, 0.8));
                    yield return (new ThemeColor(ColorSchemeIndex.Dark1, 0.35));
                    yield return (new ThemeColor(ColorSchemeIndex.Light1, -0.15));
                    foreach (ColorSchemeIndex i in Indexes2)
                        yield return (new ThemeColor(i, 0.6));
                    yield return (new ThemeColor(ColorSchemeIndex.Dark1, 0.25));
                    yield return (new ThemeColor(ColorSchemeIndex.Light1, -0.25));
                    foreach (ColorSchemeIndex i in Indexes2)
                        yield return (new ThemeColor(i, 0.4));
                    yield return (new ThemeColor(ColorSchemeIndex.Dark1, 0.15));
                    yield return (new ThemeColor(ColorSchemeIndex.Light1, -0.35));
                    foreach (ColorSchemeIndex i in Indexes2)
                        yield return (new ThemeColor(i, -0.25));
                    yield return (new ThemeColor(ColorSchemeIndex.Dark1, 0.05));
                    yield return (new ThemeColor(ColorSchemeIndex.Light1, -0.5));
                    foreach (ColorSchemeIndex i in Indexes2)
                        yield return (new ThemeColor(i, -0.5));
                }
                yield return (new RgbColor(Colors.DarkRed));
                yield return (new RgbColor(Colors.Red));
                yield return (new RgbColor(Colors.Orange));
                yield return (new RgbColor(Colors.Yellow));
                yield return (new RgbColor(Colors.LightGreen));
                yield return (new RgbColor(Colors.Green));
                yield return (new RgbColor(Colors.LightBlue));
                yield return (new RgbColor(Colors.Blue));
                yield return (new RgbColor(Colors.DarkBlue));
                yield return (new RgbColor(Colors.Purple));
                yield return (new AutomaticColor(true));
            }
        }
        static readonly ColorSchemeIndex[] Indexes = new ColorSchemeIndex[]
            {
                ColorSchemeIndex.Dark1,
                ColorSchemeIndex.Light1,
                ColorSchemeIndex.Dark2,
                ColorSchemeIndex.Light2,
                ColorSchemeIndex.Accent1,
                ColorSchemeIndex.Accent2,
                ColorSchemeIndex.Accent3,
                ColorSchemeIndex.Accent4,
                ColorSchemeIndex.Accent5,
                ColorSchemeIndex.Accent6,
            };
        static readonly ColorSchemeIndex[] Indexes2 = new ColorSchemeIndex[]
            {
                ColorSchemeIndex.Dark2,
                ColorSchemeIndex.Light2,
                ColorSchemeIndex.Accent1,
                ColorSchemeIndex.Accent2,
                ColorSchemeIndex.Accent3,
                ColorSchemeIndex.Accent4,
                ColorSchemeIndex.Accent5,
                ColorSchemeIndex.Accent6,
            };

        public void ApplyRule(StandardConditionalFormattingRule rule)
        {
            if (this.IsDirty)
            rule.Background = this.Background;
        }
    }

}